﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class PartsCatheringDAL
    {
        public static int Add(PartsCatheringInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsCathering_Add");
            DB.AddInParameter(dbCommand, "CardCode", DbType.String, obj.CardCode);
            DB.AddInParameter(dbCommand, "CardName", DbType.String, obj.CardName);
            DB.AddInParameter(dbCommand, "ShoukuanRiQi", DbType.DateTime, obj.ShoukuanRiQi);
            DB.AddInParameter(dbCommand, "JinE", DbType.Decimal, obj.JinE);
            DB.AddInParameter(dbCommand, "KuanXiangLeiXing", DbType.String, obj.KuanXiangLeiXing);
            DB.AddInParameter(dbCommand, "RealName", DbType.String, obj.RealName);
            DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);

            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(int Cid)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsCathering_Delete", Cid);
            try
            {
                int i=Convert.ToInt32(  DB.ExecuteScalar(dbCommand));
                return i>0;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetList(int Cid)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsCathering_GetList", Cid);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTableCollection GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsCathering_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String, whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTableCollection dt = DB.ExecuteDataSet(dbCommand).Tables;

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
        public static bool Update(PartsCatheringInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsCathering_Update");
            DB.AddInParameter(dbCommand, "Cid", DbType.Int32, obj.Cid);
            DB.AddInParameter(dbCommand, "ShoukuanRiQi", DbType.DateTime, obj.ShoukuanRiQi);
            DB.AddInParameter(dbCommand, "JinE", DbType.Decimal, obj.JinE);
            DB.AddInParameter(dbCommand, "KuanXiangLeiXing", DbType.String, obj.KuanXiangLeiXing);
            DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);

            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
    }
}
